package com.briup.mt.service.impl;

import com.briup.mt.bean.Greenhouse;
import com.briup.mt.bean.GreenhouseExample;
import com.briup.mt.dao.GreenhouseMapper;
import com.briup.mt.service.GreenhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class GreenhouseServiceimpl implements GreenhouseService {
    @Resource
    private GreenhouseMapper greenhouseMapper;

    @Override
    public List<Greenhouse> query(String name,String datasetId) {
        GreenhouseExample example = new GreenhouseExample();
        if(!StringUtils.isEmpty(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        if(datasetId != null){
            example.createCriteria().andAirHumidityEqualTo(datasetId);
        }
        return greenhouseMapper.selectByExample(example);
    }

    @Override
    public void saveOrUpdate(Greenhouse greenhouse) {
        // 如果userInfo中存在id属性，表示更新；否则保存
        if(greenhouse.getId() == null){
            greenhouse.setCreateTime(new Date().getTime());
            greenhouseMapper.insert(greenhouse);
        } else {
            greenhouseMapper.updateByPrimaryKey(greenhouse);
        }
    }

    @Override
    public void deleteById(long id) {
        greenhouseMapper.deleteByPrimaryKey(id);
    }
}
